package com.looeyWei;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener {

    static final int SCREEN_WIDTH = 600;
    static final int SCREEN_HEIGHT = 600;
    static final int UNIT_SIZE = 25;
    static final int RUNNING = 1;
    static final int PAUSE = 2;
    static final int GAMEOVER = 3;
    /**
     * 一共多少个单元
     */
    static final int GAME_UNITS = (SCREEN_WIDTH*SCREEN_HEIGHT)/UNIT_SIZE;
    /**
     * 蛇移动的时间间隔
     */
    static final int DELAY = 75;
    /**
     * 蛇的身体
     */
    int[] x;
    int[] y;
    /**
     * 蛇的长度
     */
    int bodyParts;
    /**
     * 被吃的苹果数量 和 苹果的位置
     */
    int applesEaten;
    int appleX;
    int appleY;
    /**
     * 蛇移动的方向
     */
    char direction;

    int gameState;
    Timer timer;
    Random random;

    GamePanel(){
        random = new Random();
        this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.setBackground(Color.BLACK);
        this.setFocusable(true);
        this.addKeyListener(new MyKeyAdapter());

        // 开始游戏
        startGame();
    }
    public void startGame(){
        x = new int[GAME_UNITS];
        y = new int[GAME_UNITS];
        direction = 'R';
        bodyParts = 6;
        applesEaten = 0;
        newApple();
        gameState = RUNNING;
        timer = new Timer(DELAY, this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        draw(g);
    }
    public void draw(Graphics g){
        if(gameState != GAMEOVER){
            // 画线条(可以去掉)
//            for (int i = 0; i < SCREEN_HEIGHT / UNIT_SIZE; i++) {
//                g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
//                g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
//            }
            // 画苹果
            g.setColor(Color.red);
            g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
            // 画蛇
            for (int i = 0; i < bodyParts; i++) {
                if(i == 0){
                    g.setColor(Color.green);
                    g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
                } else {
                    g.setColor(new Color(45, 180, 0));
                    g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
                }
            }
            // Score
            g.setColor(Color.red);
            g.setFont(new Font("Ink Free", Font.BOLD, 40));
            FontMetrics metrics1 = getFontMetrics(g.getFont());
            g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Score: " + applesEaten))/2, g.getFont().getSize());
            if(gameState == PAUSE){
                // PAUSE
                g.setColor(Color.red);
                g.setFont(new Font("Ink Free", Font.BOLD, 75));
                FontMetrics metrics2 = getFontMetrics(g.getFont());
                g.drawString("PAUSE", (SCREEN_WIDTH - metrics2.stringWidth("PAUSE"))/2, SCREEN_HEIGHT/2);
                repaint();
            }
        } else {
            gameOver(g);
        }
    }
    public void newApple(){
        appleX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
        appleY = random.nextInt((int)(SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
    }
    public void move(){
        for (int i = bodyParts; i > 0; i--) {
            x[i] = x[i-1];
            y[i] = y[i-1];
        }
        switch (direction){
            case 'U':
                y[0] = y[0] - UNIT_SIZE;
                break;
            case 'D':
                y[0] = y[0] + UNIT_SIZE;
                break;
            case 'L':
                x[0] = x[0] - UNIT_SIZE;
                break;
            case 'R':
                x[0] = x[0] + UNIT_SIZE;
                break;
            default:
                break;
        }
    }
    /**
     * 吃苹果
     */
    public void checkApple(){
        if(x[0] == appleX && y[0] == appleY){
            bodyParts ++;
            applesEaten ++;
            newApple();
        }
    }
    public void checkCollisions(){
        // 蛇头是否与身体相撞
        for (int i = bodyParts; i > 0; i--) {
            if (x[0] == x[i] && y[0] == y[i]) {
                gameState = GAMEOVER;
                break;
            }
        }
        // 与边界相撞
        if(x[0] < 0){
            gameState = GAMEOVER;
        }
        if(x[0] > SCREEN_WIDTH - UNIT_SIZE){
            gameState = GAMEOVER;
        }
        if(y[0] < 0){
            gameState = GAMEOVER;
        }
        if(y[0] > SCREEN_HEIGHT - UNIT_SIZE){
            gameState = GAMEOVER;
        }
    }
    public void gameOver(Graphics g){
        timer.stop();
        // Score
        g.setColor(Color.red);
        g.setFont(new Font("Ink Free", Font.BOLD, 40));
        FontMetrics metrics1 = getFontMetrics(g.getFont());
        g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Score: " + applesEaten))/2, g.getFont().getSize());

        // Game Over
        g.setColor(Color.red);
        g.setFont(new Font("Ink Free", Font.BOLD, 75));
        FontMetrics metrics2 = getFontMetrics(g.getFont());
        g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over"))/2, SCREEN_HEIGHT/2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(gameState == RUNNING){
            move();
            checkApple();
            checkCollisions();
        }
        repaint();
    }

    public class MyKeyAdapter extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);

            switch (e.getKeyCode()){
                case KeyEvent.VK_LEFT:
                    if(direction != 'R'){
                        direction = 'L';
                    }
                    break;
                case KeyEvent.VK_RIGHT:
                    if(direction != 'L'){
                        direction = 'R';
                    }
                    break;
                case KeyEvent.VK_UP:
                    if(direction != 'D'){
                        direction = 'U';
                    }
                    break;
                case KeyEvent.VK_DOWN:
                    if(direction != 'U'){
                        direction = 'D';
                    }
                    break;
                case KeyEvent.VK_SPACE:
                    if(gameState == RUNNING){
                        gameState = PAUSE;
                        timer.stop();
                        repaint();
                    } else if(gameState == PAUSE){
                        gameState = RUNNING;
                        timer.restart();
                    } else if(gameState == GAMEOVER){
                        startGame();
                    }
                default:
                    break;
            }
        }
    }
}
